#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#$Id: setup.py 933 2010-02-14 11:28:40Z lance $
#-------------------------------------------------------------------------------
"""distutils setup file."""
__author__ = ('Lance Finn Helsten',)
__version__ = '1.0'
__copyright__ = """Copyright (C) 2010 Lance Finn Helsten"""
__notice__ = """
This program comes with ABSOLUTELY NO WARRANTY; for details see GNU General
Public License (http://www.gnu.org/licenses/gpl.html).
"""
__license__ = """
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import sys
if sys.version_info < (3, 1):
    raise Exception("pydistrelease requires Python 3.1 or higher.")
import os
import unittest
from distutils.core import setup
from distutils.core import Command


class test(Command):
    description = "Run all unit tests and integration tests on the system."
    user_options = [
        ("suite=", "s", "Run specific test suite [default: all tests]."),
    ]
    test_dir = 'test'
    
    def initialize_options(self):
        self.suite = []
        test.test_dir = os.path.expanduser(test.test_dir)
    
    def finalize_options(self):
        for dirpath, dirnames, filenames in os.walk(test.test_dir):            
            tests = [os.path.join(dirpath, f) for f in filenames if f.endswith('Test.py')]
            tests = [os.path.abspath(f) for f in tests]
            tests = [f for f in tests if os.path.exists(f) and os.path.isfile(f)]
            tests = [os.path.split(f)[1] for f in tests]
            tests = [os.path.splitext(f)[0] for f in tests]
            self.suite.extend(tests)
            for d in [d for d in dirnames if d.startswith('.')]:
                dirnames.remove(d)
            sys.path.extend([os.path.join(dirpath, d) for d in dirnames])    
        self.suite = unittest.TestLoader().loadTestsFromNames(self.suite)        
    
    def run(self):
        tr = unittest.TextTestRunner()
        tr.run(self.suite)

def scripts():
    """Find all the scripts in the python source area."""
    base = 'src'
    ret = [f for f in os.listdir(base) if os.path.splitext(f)[1] == '.py']
    ret = [os.path.join(base, f) for f in ret]
    ret = [f for f in ret if os.path.isfile(os.path.abspath(f))]
    return ret

def packages():
    """Find all buildable packages in the python source area."""
    ret = []
    lib = 'src'
    for dir, names, files in os.walk(lib):
        if '__init__.py' in files:
            package = dir.replace(lib, '')
            if package.startswith(os.sep):
                package = package[1:]
            package = package.replace(os.sep, '.')
            ret.append(package)
    return ret



setup(
    name='pydistrelease',
    version=__version__,
    description='Python distutils release command.',
    url='http://code.google.com/p/pydistrelease/',
    author='Lance Finn Helsten',
    author_email='lanhel@gmail.com',
    scripts=scripts(),
    packages=packages(),
    package_dir={'' : 'src'},
    package_data = {'': []},
    requires=[],
    cmdclass = {"test":test},
)


